package com.hsnn.medstgmini.settlement.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.model.form.DicAreaForm;
import com.hsnn.medstgmini.base.dict.service.DicAreaManager;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.settlement.model.form.SetPaymentPayDetailForm;
import com.hsnn.medstgmini.settlement.service.SettlementPsManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.AreaUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @category 配送企业查看结算单
 * @author 徐国鹏
 * @date 2015年7月29日 上午9:27:22
 */
@Controller
@RequestMapping("/settlementPs")

public class SettlementPsController {
	
	@Autowired
	private DicAreaManager dicAreaManager;
	
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private SettlementPsManager settlementPsManager;
	
	@Value("#{sysProp['system.province']}")
	private String sysProvince;
	
	/**
	 * @category 跳转配送企业查看结算单列表（配送企业端）
	 * @Auther 徐国鹏
	 * @date 2015年7月29日 上午9:41:49
	 * @return String
	 */
	@RequestMapping("/toSettlementPs")
	public String toSettlementPs(){
		return "/settle/psSettleList";
	}
	/**
	 * @category 获取配送企业查看结算单列表
	 * @Auther 徐国鹏
	 * @date 2015年7月29日 上午10:04:52
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/getSettlementPsList")
	@ResponseBody
	public Pagination getSettlementPsList(SetPaymentPayDetailForm form) {
		Pagination page = new Pagination(request);
	    if(null != form.getPayTime() && !"".equals(form.getPayTime())){
	    	String payTime = form.getPayTime()+ " 00:00:00";
	    	form.setPayTime(payTime);
	    }
	    if(null != form.getSubmitEndDatetime() && !"".equals(form.getSubmitEndDatetime())){
	    	String submitEndDateTime = form.getSubmitEndDatetime()+ " 23:59:59";
	    	form.setSubmitEndDatetime(submitEndDateTime);
	    }
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String compId = "";// 核算中心Id
		if (user.getMain() == Whether.yes.getKey()) {
			compId = user.getOrgId();
		} else { // 非主帐号获取主帐号帐户信息
			compId = user.getRootUser().getOrgId();
		}
		page.getConditions().put("compId", compId);
		page.setForm(form);
		return settlementPsManager.getSettlementPsList(page);
	}

	/**
	 * @category 点击医疗机构数目跳转--医疗机构支付信息（配送企业端）
	 * @Auther 徐国鹏
	 * @date 2015年7月29日 下午1:33:30
	 * @return String
	 */
	@RequestMapping("/toPsHospSettleList")
	public String toPsHospSettleList(Model model, String paymentBillId,String paymentBillCode,String summoney){
		DicAreaForm area = dicAreaManager.getDicAreaById(sysProvince);
		model.addAttribute("paymentBillId", paymentBillId);
		model.addAttribute("paymentBillCode", paymentBillCode);
		model.addAttribute("summoney", ParseNumber.toStrDecimal(ParseNumber.toDouble(summoney)));
		model.addAttribute("area", area);
		model.addAttribute("sysAreaId", sysProvince);
		return "/settle/psHospSettleList";
	}
	
	/**
	 * @category 获得医疗机构支付信息
	 * @Auther 徐国鹏
	 * @date 2015年7月29日 下午3:42:46
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/getPsHospSettleList")
	@ResponseBody
	public Pagination getPsHospSettleList(SetPaymentPayDetailForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String compId = "";
		if (user.getMain() == Whether.yes.getKey()) {
			compId = user.getId();
		} else { 
			compId = user.getRootUser().getId();
		}
		
		String areaId=AreaUtil.getAreaStartCode(sysProvince,request.getParameter("area1"),request.getParameter("area2"));

		if(StringUtils.isNotBlank(areaId)){
			page.getConditions().put("areaId", areaId);
		}
		page.getConditions().put("compId", compId);
		page.getConditions().put("paymentBillId", request.getParameter("paymentBillId"));
		page.setForm(form);
		return settlementPsManager.getPsHospSettleList(page);
	}
	/**
	 * @category 药品信息（配送企业端）
	 * @Auther 徐国鹏
	 * @date 2015年7月29日 下午3:52:38
	 * @return String
	 */
	@RequestMapping("/toPsDrugSettleList")
	public String toPsDrugSettleList(Model model, SetPaymentPayDetailForm form,String hospName ,String summoney){
		String paymentBillId = request.getParameter("paymentBillId");
		String hospNameStr="";
		try {
			 hospNameStr =new String(hospName.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		
		model.addAttribute("form", form);
		model.addAttribute("paymentBillId",paymentBillId);
		model.addAttribute("summoney", ParseNumber.toStrDecimal(ParseNumber.toDouble(summoney)));
		model.addAttribute("hospName",hospNameStr);
		return "/settle/psDrugSettleList";
	}
	/**
	 * @category 获取药品信息
	 * @Auther 徐国鹏
	 * @date 2015年7月29日 下午4:22:23
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/getPsDrugSettleList")
	@ResponseBody
	public Pagination getPsDrugSettleList(SetPaymentPayDetailForm form) {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String compId = "";
		if (user.getMain() == Whether.yes.getKey()) {
			compId = user.getId();
		} else { 
			compId = user.getRootUser().getId();
		}
		page.getConditions().put("compId", compId);
		page.getConditions().put("paymentBillId", request.getParameter("paymentBillId"));
		page.getConditions().put("hospId", request.getParameter("hospId"));
		page.setForm(form);
		return settlementPsManager.getPsDrugSettleList(page);
	}
	
	/**
	 * @category 点击药品数目跳转到药品目录
	 * @Auther 周奇
	 * @date 2015年8月5日08:51:35
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/toPsDrugSettleListForPayment")
	public String psDrugSettleListForPayment(Model model, String paymentBillId,String paymentBillCode,String summoney){
		model.addAttribute("paymentBillId", paymentBillId);
		model.addAttribute("paymentBillCode", paymentBillCode);
		model.addAttribute("summoney", ParseNumber.toStrDecimal(ParseNumber.toDouble(summoney)));
		return "/settle/psDrugSettleListForPayment";
	}
	
	/**
	 * @category 获取药品信息
	 * @Auther 周奇
	 * @date 2015年8月4日19:17:34
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/getPsDrugSettleListForPayment")
	@ResponseBody
	public Pagination getPsDrugSettleListForPayment() {
		Pagination page = new Pagination(request);
		String paymentBillId = request.getParameter("paymentBillId");
		String drugCode = request.getParameter("drugCode");
		String drugName = request.getParameter("drugName");
		
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String compId = "";
		if (user.getMain() == Whether.yes.getKey()) {
			compId = user.getId();
		} else { 
			compId = user.getRootUser().getId();
		}
		
		page.getConditions().put("compId", compId);
		if(null != paymentBillId && !"".equals(paymentBillId)){
			page.getConditions().put("paymentBillId", paymentBillId);
		}
		if(null != drugCode && !"".equals(drugCode)){
			page.getConditions().put("drugCode", drugCode);
		}
		if(null != drugName && !"".equals(drugName)){
			page.getConditions().put("drugName", drugName);
		}
		return settlementPsManager.getPsDrugSettleListForPayment(page);
	}
	
	/**
	 * @category 跳转到医疗机构信息列表
	 * @Auther 周奇
	 * @date 2015年8月5日14:37:12
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/toPsHospSettlelistForDrugpage")
	public String toPsHospSettlelistForDrugpage(Model model,String paymentBillId, String drugId){
		model.addAttribute("paymentBillId", paymentBillId);
		model.addAttribute("drugId", drugId);
		return "/settle/psHospSettlelistForDrugpage"; 
	}
	/**
	 * @category 点击医疗机构目录获取医疗机构信息
	 * @Auther 周奇
	 * @date 2015年8月5日14:36:43
	 * @param form
	 * @return Pagination
	 */
	@RequestMapping("/getPsHospSettlelistForDrugpage")
	@ResponseBody
	public Pagination getPsHospSettlelistForDrugpage() {
		Pagination page = new Pagination(request);
		SysUser user = (SysUser) request.getSession().getAttribute(
				Constants.USERINFO);
		String compId = "";
		if (user.getMain() == Whether.yes.getKey()) {
			compId = user.getId();
		} else { 
			compId = user.getRootUser().getId();
		}
		page.getConditions().put("compId", compId);//企业id
		if(StringUtils.isNotBlank(request.getParameter("paymentBillId"))){
			page.getConditions().put("paymentBillId", request.getParameter("paymentBillId"));//支付单id
		}
		if(StringUtils.isNotBlank(request.getParameter("drugId"))){
			page.getConditions().put("drugId", request.getParameter("drugId"));//药品id
		}
		if(StringUtils.isNotBlank(request.getParameter("hospCode"))){
			page.getConditions().put("hospCode", request.getParameter("hospCode"));//医疗机构编码
		}
		if(StringUtils.isNotBlank(request.getParameter("hospName"))){
			page.getConditions().put("hospName", request.getParameter("hospName"));//医疗机构名称
		}
		return settlementPsManager.getPsHospSettleListForDrugpage(page);
	}
}
